package pers.lmd.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pers.lmd.domain.R;
import pers.lmd.common.constant.ResultEnum;
import pers.lmd.service.IStudentService;
import pers.lmd.service.ITeacherService;
import pers.lmd.service.IUserService;
import pers.lmd.common.utils.FileCustomUtil;
import pers.lmd.common.utils.ResultUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author lmd
 * @Date 2023/4/30 12:35
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system")
@Tag(name = "文件控制器")
public class FileController {

    private final FileCustomUtil fileCustomUtil;
    private final IUserService IUserService;
    private final IStudentService IStudentService;
    private final ITeacherService ITeacherService;

    @Operation(summary = "头像上传")
    @PostMapping("/upload")
    public R<Void> uploadAvatar(MultipartFile file) throws IOException {
        // 上传用户头像
        fileCustomUtil.uploadAvatar(file);
        return ResultUtil.success(ResultEnum.SUCCESS);
    }

    @Operation(summary = "头像回显")
    @GetMapping("/avatar")
    public void getAvatar(HttpServletResponse response) {
        // 获取用户头像
        String flag = "";
        int role = IUserService.getRole();
        if (role == 3) {
            flag = IStudentService.getAvatar();
        }
        if (role == 2) {
            flag = ITeacherService.getAvatar();
        }
        fileCustomUtil.getAvatar(response, flag);
    }

    @Operation(summary = "照片回显")
    @GetMapping("/picture")
    public void getPicture(HttpServletResponse response) {
        // 获取用户照片
        String flag = "";
        int role = IUserService.getRole();
        if (role == 3) {
            flag = IStudentService.getPicture();
        }
        fileCustomUtil.getPicture(response, flag);
    }



}